package ocbn.remotelab.util.date;

import java.util.Calendar;
import net.rim.device.api.i18n.SimpleDateFormat;
import ocbn.remotelab.exceptions.DateFormatException;

/**
 * A class that represents the date. This class extends the standard Java Date
 * class and adds date-string conversion methods.
 *
 * @author Ke Li
 */
public class Date extends java.util.Date {

    /**
     * The format that the date will be formatted in when it is converted to a
     * string.
     */

    private static SimpleDateFormat dateFormat
            = new SimpleDateFormat("MMM d, yyyy hh:mm:ss aa");

    /**
     * Constructs a Date object from no parameters. Set the date to the current
     * date and time.
     */

    public Date(){
        this(Calendar.getInstance().getTime());
    }

    /**
     * Constructs a Date object from a java.util.Date object. Set the date to
     * the value represented by the java.util.Date object. 
     *
     * @param date  A java.util.Date object
     */

    public Date(java.util.Date date){
        this(date.getTime());
    }

    /**
     * Constructs a Date object from the number of milliseconds since January 1,
     * 1970 00:00:00 GMT. 
     *
     * @param date  The number of milliseconds since January 1, 1970 00:00:00
     * GMT
     */

    public Date(long date){
        super();
        this.setTime(date);
    }

    /**
     * Converts this Date to a String.
     *
     * @return  A String representation of this Date
     */

    public String toString(){
        return dateFormat.format(this);
    }

    /**
     * Converts a String to a Date object.
     *
     * @param dateString            The string representation of a date
     * @return                      A Date object represented by the argument
     * @throws DateFormatException  Indicate that the the string is not in the
     *                              correct format and cannot be parsed as a
     *                              Date
     */

    public static Date valueOf(String dateString) throws DateFormatException{
        return new DateParser(dateFormat, dateString).getDate();
    }
}
